package com.example.smartcity_poverty.ui.AllService.SeekHouse

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.MenuItem
import android.view.inputmethod.EditorInfo
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.smartcity_poverty.Base.BaseRecyclerAdapter
import com.example.smartcity_poverty.R
import com.example.smartcity_poverty.logic.Repository.api
import com.example.smartcity_poverty.logic.Repository.coroutine
import com.example.smartcity_poverty.logic.ServiceBean.SeekHouseBean.SHHotListAllModel
import com.example.smartcity_poverty.logic.util.Util.glide
import com.example.smartcity_poverty.logic.util.Util.show
import com.example.smartcity_poverty.ui.AllService.SeekHouse.adapter.SHHotListAdapter
import com.youth.banner.adapter.BannerImageAdapter
import com.youth.banner.holder.BannerImageHolder
import com.youth.banner.indicator.CircleIndicator
import kotlinx.android.synthetic.main.activity_seek_house.*
import kotlinx.android.synthetic.main.re_item_more.*
import kotlinx.coroutines.Dispatchers


/**
 * 找房子功能实现
 */
class SeekHouseActivity : AppCompatActivity() {

    // bannerData
    val bannerList = ArrayList<String>().apply {
        this.add("http://124.93.196.45:10001/prod-api/profile/upload/image/2021/05/17/71ac2d26-4504-412d-81f1-0749f64b42d7.png")
        this.add("http://124.93.196.45:10001/prod-api/profile/upload/image/2021/05/17/ca7d9a19-143d-4579-9cbe-574bb3be737f.jpg")
        this.add("http://124.93.196.45:10001/prod-api/profile/upload/image/2021/05/17/8abdd6f4-0014-4a30-8ea0-b06835c0493a.png")
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        supportActionBar?.setDisplayHomeAsUpEnabled(true)
        setContentView(R.layout.activity_seek_house)
        title = "找房子"

        // 轮播图设置
        setBanner()

        // 房源信息
        setSeekHouse()

        upDataHotList("二手")

        // 最新房源信息
        upDataSize()

        // 实现搜索
        setSeek()
    }

    /**
     * 实现搜索
     */
    private val arrayList = ArrayList<SHHotListAllModel.RowsBean>()
    val arrMore = ArrayList<SeekHouseEntity>()

    private fun setSeek() {
        shhSeek.setOnEditorActionListener { _, i, _ ->
            if (i == EditorInfo.IME_ACTION_SEARCH) {
                arrayList.clear()
                if (shhSeek.text.toString().isNotEmpty()) {
                    coroutine(Dispatchers.Main) {
                        api.getSHSeek(shhSeek.text.toString()).apply {
                            if (this.code == 200) {
                                arrayList.addAll(this.rows!!)
                                runOnUiThread {
                                    shHomeHotRv.apply {
                                        this.layoutManager = LinearLayoutManager(context)
                                        this.adapter = SHHotListAdapter(
                                            arrayList,
                                            R.layout.re_sh_home_hot_list
                                        )
                                        this.adapter?.notifyItemChanged(arrayList.size)
                                    }
                                }
                            } else runOnUiThread { this.msg?.show() }

                        }
                    }
                }

            }
            false
        }
    }

    /**
     * 房源信息
     */


    private fun setSeekHouse() {
        arrMore.add(SeekHouseEntity(R.mipmap.ic_launcher_round, "二手"))
        arrMore.add(SeekHouseEntity(R.mipmap.ic_launcher_round, "租房"))
        arrMore.add(SeekHouseEntity(R.mipmap.ic_launcher_round, "楼盘"))
        arrMore.add(SeekHouseEntity(R.mipmap.ic_launcher_round, "中介"))

        // recyclerView适配器
        shHomeHotRv.apply {
            this.layoutManager = GridLayoutManager(context, 4)
            this.adapter = SeekHouseAdapter(arrMore, R.layout.re_item_more)
        }
    }

    inner class SeekHouseEntity(val ImageId: Int, val SeekHouseName: String)

    inner class SeekHouseAdapter(
        val list: MutableList<SeekHouseEntity>,
        layoutId: Int
    ) : BaseRecyclerAdapter<SeekHouseEntity>(list, layoutId) {
        override fun setData(
            view: BaseViewHolder.ViewFind,
            data: SeekHouseEntity,
            position: Int,
            holder: BaseViewHolder
        ) {
            val d = list[position]
            holder.viewFind.moreImage.setImageResource(d.ImageId)
            holder.viewFind.moreTitle.text = d.SeekHouseName

            holder.itemView.setOnClickListener {
                upDataHotList(d.SeekHouseName)
            }
        }

    }

    private fun upDataHotList(HotName: String) {
        coroutine(Dispatchers.IO) {
            arrayList.clear()
            if (arrayList.isEmpty()) {
                api.getSHHotListAll(HotName).apply {
                    if (this.code == 200) {
                        arrayList.addAll(this.rows!!)
                        runOnUiThread {
                            shHomeHotListRv.apply {
                                this.layoutManager = LinearLayoutManager(context)
                                this.adapter =
                                    SHHotListAdapter(arrayList, R.layout.re_sh_home_hot_list)
                                this.adapter?.notifyItemChanged(arrayList.size)
                            }
                        }

                    } else runOnUiThread { this.msg?.show() }
                }
            }
        }

    }


    /**
     * 房源最新发布
     */
    private val arrayUpDataList = ArrayList<SHHotListAllModel.RowsBean>()
    private fun upDataSize() {
        coroutine(Dispatchers.IO) {
            api.getSHHotListAll("楼盘").apply {
                if (this.code == 200) {
                    runOnUiThread {
                        arrayUpDataList.addAll(this.rows!!)
                        shHomeListRv.apply {
                            this.isNestedScrollingEnabled = false
                            this.layoutManager = LinearLayoutManager(context)
                            this.adapter =
                                SHHotListAdapter(arrayUpDataList, R.layout.re_sh_home_hot_list)
                            this.adapter?.notifyItemChanged(arrayUpDataList.size)
                        }
                    }
                } else runOnUiThread { this.msg?.show() }
            }
        }

    }


    /**
     * 轮播图设置
     */
    private fun setBanner() {
        shHomeBanner.apply {
            this.adapter = object : BannerImageAdapter<String>(bannerList) {
                override fun onBindView(p0: BannerImageHolder?, p1: String?, p2: Int, p3: Int) {
                    glide(p1.toString(), p0?.imageView!!)
                }
            }
            this.addBannerLifecycleObserver(this@SeekHouseActivity).indicator =
                CircleIndicator(context)
        }
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        finish()
        return super.onOptionsItemSelected(item)
    }
}